﻿using System.Collections.Generic;
using System.Linq;
using HK.Editor.Common;
using UnityEditor;
using UnityEngine;

namespace HK.Editor.GUIEx.Helper
{
    /// <summary>
    /// GUI Style一览
    /// </summary>
    public class GUIStyleViewer : EditorWindow {
 
        private Vector2 scrollVector2 = Vector2.zero;
        string search = "";
        GUIStyle textStyle;
  
        private static GUIStyleViewer window;

#if UGUIEX_HELPER_ENABLE
        [MenuItem(EditorConst.MENU_TXT_UNITY_GUI_STYLE_VIEW, false, EditorConst.MENU_PRIORITY_UNITY_GUI_STYLE_VIEW)]
#endif
        private static void OpenStyleViewer()
        {
            window = GetWindow<GUIStyleViewer>(false, "Unity内置样式");
        }

        private void InitGuiStyles()
        {
            _totalStyles = GUI.skin.customStyles.ToList();
            _totalStyles.Add(GUI.skin.box);
            _totalStyles.Add(GUI.skin.label);
            _totalStyles.Add(GUI.skin.textField);
            _totalStyles.Add(GUI.skin.textArea);
            _totalStyles.Add(GUI.skin.button);
            _totalStyles.Add(GUI.skin.toggle);
            _totalStyles.Add(GUI.skin.window);
            _totalStyles.Add(GUI.skin.horizontalSlider);
            _totalStyles.Add(GUI.skin.horizontalSliderThumb);
            _totalStyles.Add(GUI.skin.verticalSlider);
            _totalStyles.Add(GUI.skin.verticalSliderThumb);
            _totalStyles.Add(GUI.skin.horizontalScrollbar);
            _totalStyles.Add(GUI.skin.horizontalScrollbarThumb);
            _totalStyles.Add(GUI.skin.horizontalScrollbarLeftButton);
            _totalStyles.Add(GUI.skin.horizontalScrollbarRightButton);
            _totalStyles.Add(GUI.skin.verticalScrollbar);
            _totalStyles.Add(GUI.skin.verticalScrollbarThumb);
            _totalStyles.Add(GUI.skin.verticalScrollbarUpButton);
            _totalStyles.Add(GUI.skin.verticalScrollbarDownButton);
            _totalStyles.Add(GUI.skin.scrollView);
        }

        private List<GUIStyle> _totalStyles = null;
        void OnGUI()
        {
            if (null == _totalStyles)
            {
                InitGuiStyles();
            }
            
            GUILayout.BeginHorizontal("HelpBox");
            GUILayout.Space(40);
            EditorGUI.BeginChangeCheck();
            var searchKey = EditorGUILayout.TextField("", search, "SearchTextField");
            GUILayout.Label("", "SearchCancelButtonEmpty");
            GUILayout.EndHorizontal();
            if (EditorGUI.EndChangeCheck())
            {
                search = searchKey;
            }
            
            scrollVector2 = GUILayout.BeginScrollView(scrollVector2);
            
            // foreach (var style in GUI.skin.customStyles)
            foreach (var style in _totalStyles)
            {
                if (style.name.ToLower().Contains(search.ToLower()))
                {
                    DrawStyleItem(style);
                }
            }
            GUILayout.EndScrollView();
        }

        void DrawStyleItem(GUIStyle iStyle)
        {
            GUILayout.BeginHorizontal("box");
            GUILayout.Space(40);
            EditorGUILayout.SelectableLabel(iStyle.name);
            GUILayout.FlexibleSpace();
            EditorGUILayout.SelectableLabel(iStyle.name, iStyle);
            GUILayout.Space(40);
            EditorGUILayout.SelectableLabel("", iStyle, GUILayout.Height(40), GUILayout.Width(40));
            GUILayout.Space(50);
            if (GUILayout.Button("复制GUIStyle名字"))
            {
                var textEditor = new TextEditor
                {
                    text = iStyle.name
                };
                textEditor.OnFocus();
                textEditor.Copy();
            }
            GUILayout.EndHorizontal();
            GUILayout.Space(10);
        }
    }
}